
public class Test {
    public static void main(String[] args) throws Exception {
        Shape s1 = new Circle(new Red());
        Shape s2 = new Square(new Blue());
        s1.draw(); // Circle in red
        s2.draw(); // Square in blue
    }

}

// 桥接模式 = “一条抽象线 + 一条实现线，组合代替继承”
// 定义： 形状（圆、方） × 颜色（红、蓝）
// 不用写 4 个类，只要 2+2=4 个最小单元
interface Color {
    String desc();
}

class Red implements Color {
    public String desc() {
        return "red";
    }
}

class Blue implements Color {
    public String desc() {
        return "blue";
    }
}

abstract class Shape {
    protected Color color; // 桥接点

    Shape(Color color) {
        this.color = color;
    }

    abstract void draw();
}

class Circle extends Shape {
    Circle(Color c) {
        super(c);
    }

    void draw() {
        System.out.println("Circle in " + color.desc());
    }
}

class Square extends Shape {
    Square(Color c) {
        super(c);
    }

    void draw() {
        System.out.println("Square in " + color.desc());
    }
}